var app = getApp(), siteinfo = require("../../../siteinfo.js");

Page({
    data: {
        lymessage: !0,
        guanzhu: !1,
        quxiaogz: !0,
        ticket: !1,
        show: !1,
        share: !1,
        Preservation: !1,
        hide: !1,
        index: 0,
        assess: [],
        page: 1,
        close: !1
    },
    onLoad: function(t) {
        var s = this, e = siteinfo.siteroot.length - 14;
        if (wx.showLoading({
            title: "正在加载中"
        }), wx.hideShareMenu(), null == t.scene) {
            var a = t.id;
            s.setData({
                id: t.id,
                code_url: siteinfo.siteroot.slice(0, e)
            });
        } else {
            a = t.scene;
            s.setData({
                id: t.scene,
                code_url: siteinfo.siteroot.slice(0, e)
            });
        }
        app.util.request({
            url: "entry/wxapp/BmList",
            cachetime: "0",
            data: {
                activity_id: a
            },
            success: function(t) {
                var e = t.data;
                0 < e.length ? s.setData({
                    bmlist: e,
                    bm_people: e.length
                }) : s.setData({
                    bmlist: 1,
                    bm_people: 0
                });
            }
        }), app.util.request({
            url: "entry/wxapp/GetTicket",
            cachetime: "0",
            data: {
                activity_id: a
            },
            success: function(t) {
                var e = t.data, a = 0;
                for (var i in e) e[i].num = 0, e[i].sy_num = Number(e[i].total_num) - Number(e[i].yg_num), 
                a += e[i].num;
                s.setData({
                    num: a,
                    tickets: e
                });
            }
        });
        var i = wx.getStorageSync("userInfo").id, n = (wx.getStorageSync("userInfo").img, 
        wx.getStorageSync("userInfo").name, wx.getStorageSync("userInfo").rz_type);
        s.setData({
            user_id: i,
            rz_type: n,
            clear_time: !1
        }), s.assess(), app.util.request({
            url: "entry/wxapp/typeList",
            cachetime: "0",
            success: function(t) {
                var e = t.data;
                s.setData({
                    typeList: e
                }), s.refresh();
            }
        });
    },
    assess: function(t) {
        var e = this, a = e.data.id, i = e.data.page, s = e.data.assess;
        app.util.request({
            url: "entry/wxapp/ActivityAssessList",
            data: {
                activity_id: a,
                page: i
            },
            success: function(t) {
                0 < t.data.length && (s = s.concat(t.data), e.setData({
                    assess: s,
                    page: i + 1
                }));
            }
        });
    },
    refresh: function(t) {
        var n = this, e = (n.data.typeList, n.data.id);
        app.util.request({
            url: "entry/wxapp/url",
            cachetime: "0",
            success: function(t) {
                n.setData({
                    url: t.data
                });
                t.data, n.data.clear_time;
                var i = app.today_time(), s = n.data.typeList;
                app.util.request({
                    url: "entry/wxapp/ActivityDetails",
                    cachetime: "0",
                    data: {
                        activity_id: e
                    },
                    success: function(t) {
                        var e = t.data;
                        for (var a in app.util.request({
                            url: "entry/wxapp/CheckAttestation",
                            cachetime: "0",
                            data: {
                                user_id: e.user_id
                            },
                            success: function(t) {
                                console.log(t), 0 != t.data && n.setData({
                                    rz_type: t.data.type
                                });
                            }
                        }), s) e.type_id == s[a].id && (e.type_id = s[a].type_name);
                        !function t(e) {
                            var a = e || [];
                            var i = Math.round(new Date().getTime() / 1e3);
                            var s = a - i || [];
                            n.setData({
                                clock: function(t) {
                                    var e = Math.floor(t), a = Math.floor(e / 3600 / 24), i = Math.floor(e / 3600 % 24), s = Math.floor(e / 60 % 60), n = Math.floor(e % 60);
                                    a < 10 && (a = "0" + a);
                                    i < 10 && (i = "0" + i);
                                    n < 10 && (n = "0" + n);
                                    s < 10 && (s = "0" + s);
                                    return {
                                        day: a,
                                        hr: i,
                                        min: s,
                                        sec: n
                                    };
                                }(s)
                            });
                            s <= 0 ? n.setData({
                                clock: "已经截止"
                            }) : 0 < s && 0 == n.data.clear_time && setTimeout(function() {
                                s -= 1e3, t(e);
                            }, 1e3);
                        }(e.start_time), e.start_time = app.ormatDate(e.start_time).slice(0, 16), e.yestoday = app.yestoday(e.start_time), 
                        e.end_time = app.ormatDate(e.end_time).slice(0, 16), 1 == e.is_close && i < e.end_time ? e.activity_over = 1 : e.activity_over = 2, 
                        "" != e.hd_imgs && (e.hd_imgs = e.hd_imgs.split(",")), 0 == e.cost ? e.cost = "免费" : e.cost = "￥" + e.cost;
                        wx.getStorageSync("url"), e.logo;
                        e.end_time = e.end_time.slice(5, 16), e.start_time = e.start_time.slice(5, 16), 
                        n.setData({
                            activity: e,
                            logo: n.data.url + e.logo
                        });
                    }
                });
            }
        }), app.util.request({
            url: "entry/wxapp/HdPoster",
            data: {
                id: e
            },
            success: function(t) {
                console.log("获取活动二维码"), console.log(t);
                n.data.code_url, t.data;
                wx.downloadFile({
                    url: n.data.code_url + t.data,
                    success: function(t) {
                        n.setData({
                            poster: t.tempFilePath
                        }), n.canvas();
                    }
                });
            }
        }), app.util.request({
            url: "entry/wxapp/CheckGz",
            cachetime: "0",
            data: {
                user_id: n.data.user_id,
                activity_id: e
            },
            success: function(t) {
                1 == t.data ? n.setData({
                    guanzhu: !0,
                    quxiaogz: !1
                }) : n.setData({
                    guanzhu: !1,
                    quxiaogz: !0
                });
            }
        });
    },
    canvas: function(t) {
        var e = this, a = 0, i = 0, s = (e.data.user_img, wx.getStorageSync("userInfo").name), n = e.data.activity;
        wx.getSystemInfo({
            success: function(t) {
                a = t.windowWidth, i = t.windowHeight, e.setData({
                    width: t.windowWidth,
                    height: t.windowHeight - 50
                });
            }
        });
        var o = a / 1080, r = 200 * (o = o.toFixed(2)), u = 720 * o, c = 780 * o, l = 410 * o, d = 990 * o, h = 1150 * o, f = 1310 * o, p = 610 * o, m = 600 * o, g = 550 * o, _ = 300 * o, v = 250 * o, w = e.data.img_width, y = e.data.img_height, x = y / w;
        if (w < a) {
            w = a;
            2;
            2;
        } else {
            if (2, 300 < y) {
                2;
            } else ;
        }
        var D = wx.createCanvasContext("firstCanvas");
        D.setFillStyle("#fff"), D.rect(0, 0, a, i), D.setFillStyle("#f9f9f9"), D.fill();
        var S = a - 40;
        D.drawImage("../img/canvas.png", 20, r, S, S * (1140 / 939)), D.drawImage("../img/biaoqian.png", 20, u), 
        D.fillStyle = "#333", D.setFontSize(18), D.fillText(s, 40, l), D.fillStyle = "#f66925", 
        D.setFontSize(12), D.fillText("邀请你一起来参加这个活动", 40, g), D.drawImage(e.data.poster, p + 10, v, _, _), 
        D.fillStyle = "#000", D.setFontSize(14), D.fillText("长按识别二维码查看", p, m), D.fillStyle = "#fff", 
        D.setFontSize(14), D.fillText("活动信息", 30, c), D.fillStyle = "#000", D.setFontSize(15), 
        D.fillText(n.title, 50, d), D.fillText(n.address, 50, h), D.fillText(n.start_time + "——" + n.end_time, 50, f), 
        D.draw();
    },
    totemp: function(t) {
        var e = this, a = this.data.width, i = this.data.height;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: a,
            height: i,
            canvasId: "firstCanvas",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        e.setData({
                            share: !1,
                            close: !1,
                            Preservation: !1
                        });
                    },
                    fail: function(t) {},
                    complete: function(t) {}
                });
            }
        });
    },
    image: function(t) {
        var e = t.detail.height, a = t.detail.width;
        this.setData({
            proportion: e / a,
            img_height: e,
            img_width: a
        });
    },
    share: function(t) {
        var e = this.data.share;
        0 == e ? this.setData({
            share: !0
        }) : this.setData({
            share: !1
        });
    },
    Preservation: function(t) {
        this.data.Preservation;
        this.setData({
            Preservation: !0,
            share: !1
        });
    },
    message: function() {
        this.setData({
            lymessage: !1
        });
    },
    messagebox: function() {
        this.setData({
            lymessage: !0
        });
    },
    messagequeding: function() {
        this.setData({
            lymessage: !0
        }), wx.showToast({
            title: "留言成功",
            icon: "success",
            duration: 2e3
        });
    },
    zhubanf: function(t) {
        var e = this.data.rz_type, a = this.data.activity;
        0 == e && 0 != a.user_id ? e = "未认证" : 1 == e && 0 != a.user_id ? e = "个人认证" : 2 == e && 0 != a.user_id ? e = "企业认证" : 0 != a.user_id && (e = "官方发布");
        t.currentTarget.dataset.user_id;
        var i = t.currentTarget.dataset.sponsor;
        0 != a.user_id && wx.navigateTo({
            url: "zhubanf?sponsor=" + i + "&rz_type=" + e
        });
    },
    delete_ticket: function(t) {
        "" == wx.getStorageSync("userInfo").link_name ? this.setData({
            per_info: !0,
            close: !1
        }) : 1 == this.data.ticket ? this.setData({
            ticket: !1,
            close: !1
        }) : this.setData({
            ticket: !0,
            close: !0
        });
    },
    link_name: function(t) {
        var e = t.detail.value;
        this.setData({
            link_name: e
        });
    },
    link_tel: function(t) {
        var e = t.detail.value;
        11 == e.length && (0 == /^1[3|4|5|7|8|9][0-9]\d{4,8}$/.test(e) ? wx.showModal({
            content: "请输入正确的手机号"
        }) : this.setData({
            link_tel: e
        }));
    },
    per_info: function(t) {
        var e = this.data.per_info;
        1 == e ? this.setData({
            per_info: !1,
            close: !1
        }) : this.setData({
            per_info: !0,
            close: !0
        });
    },
    save_perinfo: function(t) {
        var e = this, a = e.data.link_tel, i = e.data.link_name, s = "";
        "" == i || null == i ? s = "请输入姓名" : "" != a && null != a || (s = "请输入您的联系方式"), 
        "" != s ? wx.showModal({
            title: "",
            content: s
        }) : app.util.request({
            url: "entry/wxapp/ModifyUserInfo",
            data: {
                user_id: wx.getStorageSync("userInfo").id,
                link_name: i,
                link_tel: a
            },
            success: function(t) {
                1 == t.data && (app.getUserInfo(function(t) {
                    e.setData({
                        userInfo: t
                    });
                }), e.setData({
                    per_info: !1,
                    close: !1
                }));
            }
        });
    },
    bmlist: function(t) {
        wx.navigateTo({
            url: "activity_list?id=" + this.data.id
        });
    },
    guanzhu: function() {
        var t = this.data.user_id, e = this.data.id;
        this.setData({
            guanzhu: !0,
            quxiaogz: !1
        }), app.util.request({
            url: "entry/wxapp/savefollow",
            cachetime: "0",
            data: {
                activity_id: e,
                user_id: t
            },
            success: function(t) {
                1 == t.data && wx.showToast({
                    title: "已关注成功",
                    icon: "success",
                    duration: 2e3
                });
            }
        });
    },
    quxiaogz: function() {
        var t = this.data.user_id, e = this.data.id;
        this.setData({
            quxiaogz: !0,
            guanzhu: !1
        }), app.util.request({
            url: "entry/wxapp/CancelFollow",
            cachetime: "0",
            data: {
                activity_id: e,
                user_id: t
            },
            success: function(t) {
                1 == t.data && wx.showToast({
                    title: "已取消关注",
                    icon: "success",
                    duration: 2e3
                });
            }
        });
    },
    openlocation: function(t) {
        var e = this.data.activity, a = this.data.activity.coordinates.split(",");
        wx.openLocation({
            latitude: Number(a[0]),
            longitude: Number(a[1]),
            scale: 28,
            name: e.title,
            address: e.address
        });
    },
    make_phone: function(t) {
        wx.makePhoneCall({
            phoneNumber: this.data.activity.link_tel
        });
    },
    reduce: function(t) {
        var e = this.data.tickets, a = t.currentTarget.dataset.index;
        if (e[a].num = e[a].num - 1, 0 <= e[a].num) {
            var i = 0;
            for (var s in e) i += e[s].num;
            this.setData({
                num: i,
                tickets: e
            });
        } else {
            i = e[a].num = 0;
            for (var n in e) i += e[n].num;
            this.setData({
                num: i,
                tickets: e
            });
        }
    },
    plus: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.tickets;
        if (a[e].limit_num <= 0) {
            var i = 0;
            for (var s in a) i += a[s].num;
            this.setData({
                num: i,
                tickets: a
            });
        } else if (a[e].num < Number(a[e].sy_num)) {
            if (a[e].num < Number(a[e].limit_num)) {
                a[e].num = a[e].num + 1;
                i = 0;
                for (var n in a) i += a[n].num;
                this.setData({
                    num: i,
                    tickets: a
                });
            }
        } else wx.showToast({
            title: "余票不足"
        });
    },
    next_step: function(t) {
        var e = this.data.tickets, a = 0;
        for (var i in e) e[i].price = Number(e[i].num) * Number(e[i].money), a += e[i].price;
        a = a.toFixed(2);
        var s = this.data.id, n = [];
        for (var o in e) 0 < e[o].num && n.push(e[o]);
        wx.setStorageSync("tickets", n), wx.navigateTo({
            url: "../fill_info/fill_info?price=" + a + "&activity_id=" + s
        });
    },
    show: function(t) {
        var e = this.data.show;
        !1 === e ? this.setData({
            show: !0,
            close: !0
        }) : this.setData({
            show: !1,
            close: !1
        });
    },
    hide: function(t) {
        var e = this, a = e.data.user_id, i = wx.getStorageSync("platform").ly_num;
        app.util.request({
            url: "entry/wxapp/GetLyNum",
            data: {
                user_id: a
            },
            success: function(t) {
                t.data < i ? !1 === e.data.hide ? e.setData({
                    hide: !0,
                    close: !0
                }) : e.setData({
                    hide: !1,
                    close: !1
                }) : wx.showModal({
                    content: "已达到留言次数上限"
                });
            }
        });
    },
    index: function(t) {
        wx.reLaunch({
            url: "../index/index"
        });
    },
    fabu: function(t) {
        wx.reLaunch({
            url: "../fabu/fabu"
        });
    },
    previewImage: function(t) {
        var e = this.data.url, a = [], i = t.currentTarget.dataset.inde, s = this.data.activity.hd_imgs;
        for (var n in s) a.push(e + s[n]);
        wx.previewImage({
            current: e + s[i],
            urls: a
        });
    },
    hide1: function(t) {
        this.setData({
            index: 0
        });
    },
    hide2: function(t) {
        this.setData({
            index: 1
        });
    },
    textarea: function(t) {
        this.setData({
            textarea: t.detail.value
        });
    },
    saves: function(t) {
        var e = this, a = e.data.textarea, i = e.data.id, s = e.data.user_id;
        "" == a || null == a ? wx.showModal({
            title: "",
            content: "请输入留言"
        }) : app.util.request({
            url: "entry/wxapp/SaveAssess",
            data: {
                activity_id: i,
                user_id: s,
                content: a
            },
            success: function(t) {
                wx.showToast({
                    title: "提交成功"
                }), e.setData({
                    hide: !1,
                    close: !1,
                    page: 1,
                    assess: []
                }), e.assess();
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        this.setData({
            xuanran: !0
        });
    },
    onHide: function() {},
    onUnload: function() {
        this.setData({
            clear_time: !0
        }), this.refresh();
    },
    onPullDownRefresh: function() {
        this.setData({
            page: 1,
            assess: []
        }), this.refresh(), wx.stopPullDownRefresh();
    },
    onReachBottom: function() {
        this.assess();
    },
    onShareAppMessage: function(t) {
        var e = this.data.activity, a = this.data.url;
        return "button" === t.from && console.log(t.target), {
            title: e.title,
            path: "zh_gjhdbm/pages/activeinfo/activeinfo?id=" + e.id,
            imageUrl: a + e.logo,
            success: function(t) {
                console.log(t);
            },
            fail: function(t) {}
        };
    }
});